package com.example.elysiumease;

import com.example.elysiumease.mapper.OperationTypeMapper;
import com.example.elysiumease.model.OperationType;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Commit;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
@Transactional
@Commit
public class OperationTypeMapperTest {

    @Autowired
    private OperationTypeMapper operationTypeMapper;

    @Test
    void testInsertOperationType() {
        OperationType operationType = new OperationType();
        operationType.setTypeName("TEST_OPERATION");

        int result = operationTypeMapper.insertOperationType(operationType);
        assertEquals(1, result);
        assertNotNull(operationType.getTypeId());
    }

    @Test
    void testUpdateOperationType() {
        // 插入一条记录
        OperationType operationType = new OperationType();
        operationType.setTypeName("TEST_OPERATION");
        operationTypeMapper.insertOperationType(operationType);

        // 更新记录
        operationType.setTypeName("UPDATED_TEST_OPERATION");
        int result = operationTypeMapper.updateOperationType(operationType);
        assertEquals(1, result);

        // 验证更新结果
        OperationType updatedOperationType = operationTypeMapper.selectById(operationType.getTypeId());
        assertEquals("UPDATED_TEST_OPERATION", updatedOperationType.getTypeName());
    }

    @Test
    void testDeleteOperationType() {
        // 插入一条记录
        OperationType operationType = new OperationType();
        operationType.setTypeName("TEST_OPERATION");
        operationTypeMapper.insertOperationType(operationType);

        // 删除记录
        int result = operationTypeMapper.deleteOperationType(operationType.getTypeId());
        assertEquals(1, result);

        // 验证删除结果
        OperationType deletedOperationType = operationTypeMapper.selectById(operationType.getTypeId());
        assertNull(deletedOperationType);
    }

    @Test
    void testSelectById() {
        // 查询记录
        OperationType dbOperationType = operationTypeMapper.selectById((short) 1);
        assertNotNull(dbOperationType);
        assertNotNull(dbOperationType.getTypeId());
    }

    @Test
    void testSelectAll() {
        // 插入两条记录
        OperationType operationType1 = new OperationType();
        operationType1.setTypeName("TEST_OPERATION_1");
        operationTypeMapper.insertOperationType(operationType1);

        OperationType operationType2 = new OperationType();
        operationType2.setTypeName("TEST_OPERATION_2");
        operationTypeMapper.insertOperationType(operationType2);

        // 查询所有记录
        List<OperationType> operationTypes = operationTypeMapper.selectAll();
        assertNotNull(operationTypes);
        assertFalse(operationTypes.isEmpty());
    }
}